package com.yunji.order.service;

import com.google.common.collect.Maps;
import com.yunji.common.utils.ConvertDataUtil;
import com.yunji.order.bo.*;
import com.yunji.order.dao.IOrderDao;
import com.yunji.order.dao.IOrderDetailDao;
import com.yunji.order.dao.IOrderOrdinalDao;
import com.yunji.order.domain.OrderDO;
import com.yunji.order.domain.OrderDetailDO;
import com.yunji.order.domain.OrderOrdinalDO;
import com.yunji.order.dto.OrderPageDTO;
import com.yunji.product.bo.ProductOrderBO;
import org.apache.commons.compress.utils.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.order.service.impl
 * @Title: OrderServiceImpl
 * @Description: 交易订单接口实现
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-04-10 14:10:15      V1.0        HuaAo       新建类
 */
@Service
public class OrderService {
    protected final Logger logs = LoggerFactory.getLogger(this.getClass());

    @Resource
    private IOrderDao orderDao;

    @Resource
    private IOrderDetailDao orderDetailDao;

    @Resource
    private IOrderOrdinalDao orderOrdinalDao;


    public boolean saveOrderOrdinals(List<OrderOrdinalDO> orderOrdinals) {
        return orderOrdinalDao.saveBatch(orderOrdinals);
    }

    public List<OrderDetailDO> getOrderDetailsByOid(Long orderId) {
        if (orderId == null)
            return Lists.newArrayList();
        return orderDetailDao.getListByOid(orderId);
    }

    public Map<Long, List<OrderDetailDO>> getDetailListMapByOds(List<Long> orderIds) {
        if (orderIds == null || orderIds.isEmpty())
            return Maps.newHashMap();
        return ConvertDataUtil.getGroupBy(() -> orderDetailDao.getListByOds(orderIds), OrderDetailDO::getOrderId);
    }

    public List<OrderDO> orderListByMember(ListOrderMemberBO memberBO) {
        return orderDao.orderListByMember(memberBO);
    }

    public List<OrderDO> orderScopeList(OrderPageDTO param) {
        return orderDao.orderPage(param);
    }

    public OrderDO getOrderById(Long id) {
        return orderDao.getById(id);
    }

    public OrderDO getOrderByCode(String orderCode) {
        return orderDao.getOrderByCode(orderCode);
    }


    public Long getOrderIdByQrcode(GetOrderQrcodeBO bo) {
        return orderDao.getOrderIdByQrcode(bo);
    }

    public boolean existsQrcodeOrder(GetOrderQrcodeBO bo) {
        return orderDao.existsQrcodeOrder(bo);
    }

    public List<OrderDO> listDeliveryOrder(ListDeliveryOrderBO bo) {
        return orderDao.listDeliveryOrder(bo);
    }

    public List<OrderDO> listScaleOrder(ListScaleOrderBO bo) {
        return orderDao.listScaleOrder(bo);
    }

    public boolean createOrder(OrderDO order) {
        return orderDao.save(order);
    }

    public boolean createOrderDetails(List<OrderDetailDO> details) {
        return orderDetailDao.saveBatch(details);
    }

    public boolean updateOrder(OrderDO order) {
        return orderDao.updateById(order);
    }


    public boolean updateOrderStatus(OrderModifyStatusBO orderStatus) {
        return orderDao.updateOrderStatus(orderStatus);
    }

    public List<OrderDO> searchOrderStatus(Integer... status) {
        return orderDao.searchOrderStatus(Arrays.asList(status));
    }

    public boolean existsOrder(ExistsOrderBO existsOrderBO) {
        return orderDao.existsOrder(existsOrderBO);
    }

    public boolean existsBySiteIds(List<Long> siteIds) {
        return orderDao.existsBySiteIds(siteIds);
    }

    public Integer countProductOrderSale(ProductOrderBO bo) {
        return orderDao.countProductOrderSale(bo);
    }

    public Long getOrderId(ExistsOrderBO build) {
        return orderDao.getOrderId(build);
    }

    public List<Integer> getOrdinalsByOrderId(Long orderId) {
        return orderOrdinalDao.getOrdinalsByOrderId(orderId);
    }

    public List<String> getOrderNamesByOrderId(Long orderId) {
        return orderDetailDao.getNamesByOrderId(orderId);
    }
}
